package com.bdqn.gulimall.member.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.common.exception.BizCodeEnum;
import com.bdqn.common.utils.PageUtils;
import com.bdqn.common.utils.R;
import com.bdqn.gulimall.member.entity.MemberEntity;
import com.bdqn.gulimall.member.exception.PhoneException;
import com.bdqn.gulimall.member.exception.UserNameExistException;
import com.bdqn.gulimall.member.feign.CouPonFeignService;
import com.bdqn.gulimall.member.service.MemberService;
import com.bdqn.gulimall.member.vo.MemberLoginVo;
import com.bdqn.gulimall.member.vo.MemberRegistVo;
import com.bdqn.gulimall.member.vo.SocialUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;



/**
 * 会员表
 *
 * @author cxk&xp&fg
 * @email 200224242@qq.com
 * @date 2022-01-20 16:23:36
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    @Resource
    private CouPonFeignService couPonFeignService;



    @PostMapping("/gitee/oauth2/login")
    public R giteelogin(@RequestBody SocialUser vo){
        MemberEntity entity=memberService.login(vo);
        if(entity!=null){
            // TODO: 2022/3/3 0003 登录成功处理
            return R.ok().setData(entity);
        }else{
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
        }
    }




    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){
        try {
            memberService.regist(vo);
        }catch (PhoneException phoneException){
            //如果出现相同手机号就抛出自定义异常
            R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UserNameExistException userNameExistException){
            //如果出现相同用户名就抛出自定义异常
            R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        System.out.println("login"+JSON.toJSONString(vo));

        MemberEntity entity=memberService.login(vo);
        if(entity!=null){

            // TODO: 2022/3/2 0002 登录成功处理
            return R.ok().setData(entity);
        }else{
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
